import Vue from 'vue'
import Vuex from 'vuex'
import * as api from '../utils/api.js'
import router from '../router'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		isLogin: false,
		token: "",
		userInfo: {},
		deptInfo: [],
	},
	mutations: {
		setDept(state, provider) {
			state.deptInfo = provider
			uni.setStorage({ // 缓存用户登陆状态
				key: 'deptInfo', 
				data: provider
			})
		},
		getDeptInfo(state) {
			if (uni.getStorageSync('deptInfo')) {
				state.deptInfo = uni.getStorageSync('deptInfo')
				return true
			} else {
				return false
			}
		},
		setToken(state, provider) {
			state.token = provider
			uni.setStorage({ // 缓存用户登陆状态
				key: 'token', 
				data: provider
			})
		},
		getTokenInfo(state) {
			if (uni.getStorageSync('token')) {
				state.token = uni.getStorageSync('token')
				return true
			} else {
				return false
			}
		},
		setUserInfo(state, provider) {
			state.userInfo = provider
			uni.setStorage({ // 缓存用户登陆状态
				key: 'userInfo',  
				data: provider
			})
		},
		getUserInfo(state) {
			if (uni.getStorageSync('userInfo')) {
				state.userInfo = uni.getStorageSync('userInfo')
				// api.tUserGetId(uni.getStorageSync('userInfo').id).then(d => {
				// 	this.commit('setUserInfo', d.data)
				// })
				return true
			} else {
				return false
			}
		},
		exitLogin(state) {
			state.userInfo = {}
			uni.removeStorageSync('userInfo')
		}
	},
	actions: {
	
	}
})

export default store
